package com.ruoyi.teaching.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.teaching.bo.YaMyStageTaskslBo;
import com.ruoyi.teaching.domain.YaStageTaskSubmit;
import com.ruoyi.teaching.domain.YaStageTasks;
import com.ruoyi.teaching.service.IYaStageTaskSubmitService;
import com.ruoyi.teaching.service.IYaStageTasksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 我的任务Controller
 * 
 * @author ZBX
 * @date 2025-03-11
 */
@RestController
@RequestMapping("/my_stage_tasks")
public class YaMyStageTasksController extends BaseController
{
    @Autowired
    private IYaStageTasksService yaStageTasksService;
    @Autowired
    private IYaStageTaskSubmitService yaStageTaskSubmitService;

    /**
     * 查询我的任务列表
     */
    @RequiresPermissions("teaching:my_stage_tasks:list")
    @GetMapping("/list")
    public TableDataInfo list(YaStageTasks yaStageTasks)
    {
        startPage();
        List<YaMyStageTaskslBo> list = yaStageTasksService.selectYaMyStageTasksList(yaStageTasks);
        return getDataTable(list);
    }

    /**
     * 提交任務
     */
    //http://localhost:82/dev-api/teaching/my_stage_tasks
    @RequiresPermissions("teaching:my_stage_tasks:add")
    @PostMapping
    public AjaxResult insertStageTaskSubmit(@RequestBody YaStageTaskSubmit yaStageTaskSubmit)
    {
        Long studentId = SecurityUtils.getUserId();
        yaStageTaskSubmit.setStudentId(studentId);
        yaStageTaskSubmit.setSubmitStatus("1");
        yaStageTaskSubmit.setSubmitBy(SecurityUtils.getUsername());
        yaStageTaskSubmit.setSubmitTime(new Date());
        yaStageTaskSubmitService.insertStageTaskSubmit(yaStageTaskSubmit);
        return AjaxResult.success();
    }

    /**
     * 修改提交
     */
    //http://localhost:82/dev-api/teaching/my_stage_tasks
    @RequiresPermissions("teaching:my_stage_tasks:edit")
    @PutMapping
    public AjaxResult editStageTaskSubmit(@RequestBody YaStageTaskSubmit yaStageTaskSubmit)
    {
        Long studentId = SecurityUtils.getUserId();
        yaStageTaskSubmit.setStudentId(studentId);
        if (yaStageTaskSubmit.getAttachment() == null || yaStageTaskSubmit.getAttachment().isEmpty()) {
            yaStageTaskSubmit.setSubmitStatus("0");
        } else {
            yaStageTaskSubmit.setSubmitStatus("1");
        }
        yaStageTaskSubmit.setUpdateBy(SecurityUtils.getUsername());
        yaStageTaskSubmit.setUpdateTime(new Date());
        yaStageTaskSubmitService.editStageTaskSubmit(yaStageTaskSubmit);
        return AjaxResult.success();
    }

}
